package com.soku.wangheng.xcoder.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;

import com.soku.wangheng.xcoder.model.Administrator;
import com.soku.wangheng.xcoder.util.JdbcUtils;

public class AdminDAO {
	
	private static final String ADMIN_TABLE_NAME = "t_admin";
	
	public Administrator matchAdmin(String userName, String pwd) {
		
		JdbcTemplate jdbcTemplate = JdbcUtils.getJdbcTemplate();
		String sql = "select * from " + ADMIN_TABLE_NAME + " where user_name = ? and pwd = ?";
		
		final Administrator administrator = new Administrator();
		jdbcTemplate.query(sql, new Object[]{userName, pwd}, new RowCallbackHandler() {
			
			public void processRow(ResultSet rs) throws SQLException {
				administrator.setId(rs.getInt("id"));
				administrator.setUserName(rs.getString("user_name"));
				administrator.setPwd(rs.getString("pwd"));
				administrator.setCreateTime(rs.getDate("create_time"));
				administrator.setLastLogin(rs.getTimestamp("last_login"));
				administrator.setRight(rs.getString("right"));
				administrator.setState(rs.getInt("state"));
			}
		});
		if(administrator.getId() == 0) {
			return null;
		}
		return administrator;
	}

}
